import { Checker, IntersectResult, Ray3, Vector3 } from "@script/index";

export default class Plane {
  private readonly d: number;
  private readonly normal: Vector3;
  private position: Vector3;
  material: Checker;

  constructor(normal: Vector3, d) {
    this.normal = normal;
    this.d = d;
  }

  copy(): Plane {
    return new Plane(this.normal.copy(), this.d);
  }

  initialize(): void {
    this.position = this.normal.multiply(this.d);
  }

  intersect(ray: Ray3): IntersectResult {
    let a = ray.direction.dot(this.normal);
    if (a >= 0) {
      return IntersectResult.noHit;
    }
    let b = this.normal.dot(ray.origin.subtract(this.position));
    let result = new IntersectResult();
    result.geometry = this;
    result.distance = -b / a;
    result.position = ray.point(result.distance);
    result.normal = this.normal;
    return result;
  }
}